{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Liu Linhao  \n",
    "刘林昊  \n",
    "320200931111   \n",
    "Lab2  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas\n",
    "import datetime"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "87442\n"
     ]
    }
   ],
   "source": [
    "file_read = pandas.read_csv('gaze.csv', usecols = ['confidence'])\n",
    "print(len(file_read[(file_read['confidence'] >= 0.9)]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "120036\n"
     ]
    }
   ],
   "source": [
    "file_read = pandas.read_csv('gaze.csv',usecols=['norm_pos_x','norm_pos_y'])\n",
    "x_mean = file_read['norm_pos_x'].mean()\n",
    "y_mean = file_read['norm_pos_y'].mean()\n",
    "x_std = file_read['norm_pos_x'].std()\n",
    "y_std = file_read['norm_pos_y'].std()\n",
    "print(len(file_read[(abs(file_read['norm_pos_x'] - x_mean) < 3 * x_std) & (abs(file_read['norm_pos_y'] - y_mean) < 3 * y_std)]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                   gaze_timestamp  world_index  confidence  norm_pos_x  \\\n",
      "0  1970-01-02T00:29:10.7767800000            0    0.828006    0.531433   \n",
      "1  1970-01-02T00:29:10.7767870000            0    0.459705    0.337051   \n",
      "2  1970-01-02T00:29:10.7797090000            0    0.888789    0.543140   \n",
      "3  1970-01-02T00:29:10.7797590000            0    0.550258    0.337923   \n",
      "4  1970-01-02T00:29:10.7874850000            1    0.502595    0.340109   \n",
      "\n",
      "   norm_pos_y       base_data  gaze_point_3d_x  gaze_point_3d_y  \\\n",
      "0    0.397509   88150.77678-0        28.369753        52.033135   \n",
      "1    0.186147  88150.776787-1      -142.869866       154.788121   \n",
      "2    0.410668  88150.779709-0        38.942051        45.359863   \n",
      "3    0.173301  88150.779759-1      -141.879169       160.867779   \n",
      "4    0.142989  88150.787485-1      -139.404860       175.088321   \n",
      "\n",
      "   gaze_point_3d_z  eye_center0_3d_x  ...  eye_center0_3d_z  gaze_normal0_x  \\\n",
      "0       705.121116         20.000000  ...        -20.000000        0.011527   \n",
      "1       684.981621        -39.934928  ...        -20.075283       -0.141761   \n",
      "2       705.232112         20.000000  ...        -20.000000        0.026087   \n",
      "3       683.893108        -39.934928  ...        -20.075283       -0.140397   \n",
      "4       681.151453        -39.934928  ...        -20.075283       -0.136989   \n",
      "\n",
      "   gaze_normal0_y  gaze_normal0_z  eye_center1_3d_x  eye_center1_3d_y  \\\n",
      "0        0.051002        0.998632               NaN               NaN   \n",
      "1        0.192518        0.971000               NaN               NaN   \n",
      "2        0.041811        0.998785               NaN               NaN   \n",
      "3        0.200891        0.969501               NaN               NaN   \n",
      "4        0.220475        0.965725               NaN               NaN   \n",
      "\n",
      "   eye_center1_3d_z  gaze_normal1_x  gaze_normal1_y  gaze_normal1_z  \n",
      "0               NaN             NaN             NaN             NaN  \n",
      "1               NaN             NaN             NaN             NaN  \n",
      "2               NaN             NaN             NaN             NaN  \n",
      "3               NaN             NaN             NaN             NaN  \n",
      "4               NaN             NaN             NaN             NaN  \n",
      "\n",
      "[5 rows x 21 columns]\n"
     ]
    }
   ],
   "source": [
    "def change_timestamp(timee):\n",
    "    ts = timee['gaze_timestamp']\n",
    "    datee = datetime.datetime.utcfromtimestamp(round(float(ts), 6))\n",
    "    time = datee.strftime('%Y-%m-%dT%H:%M:%S.%f0000')\n",
    "    return time\n",
    "\n",
    "file_read = pandas.read_csv('gaze.csv')\n",
    "file_read['gaze_timestamp'] = file_read.apply(change_timestamp,axis=1)\n",
    "print(file_read.head())\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "428  Hz\n"
     ]
    }
   ],
   "source": [
    "file_read = pandas.read_csv(\"gaze.csv\",usecols = ['gaze_timestamp'])\n",
    "print(round(len(file_read) / (file_read['gaze_timestamp'][len(file_read)-1] - file_read['gaze_timestamp'][0])), \" Hz\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   gaze_timestamp  world_index  confidence  norm_pos_x  norm_pos_y  \\\n",
      "0    88150.776780          0.0    0.828006    0.531433    0.397509   \n",
      "1    88150.787485          1.0    0.502595    0.340109    0.142989   \n",
      "2    88150.793583          1.0    0.625134    0.495993    0.399021   \n",
      "3    88150.801140          1.0    0.708476    0.495145    0.408338   \n",
      "4    88150.812873          2.0    0.856341    0.494809    0.402125   \n",
      "\n",
      "                           base_data  gaze_point_3d_x  gaze_point_3d_y  \\\n",
      "0                      88150.77678-0        28.369753        52.033135   \n",
      "1                     88150.787485-1      -139.404860       175.088321   \n",
      "2  88150.79569-0 88150.79147699999-1         1.533100       -21.730862   \n",
      "3      88150.801157-0 88150.801123-1         1.782444       -18.930993   \n",
      "4      88150.809749-0 88150.815997-1         1.914321       -20.302480   \n",
      "\n",
      "   gaze_point_3d_z  eye_center0_3d_x  ...  eye_center0_3d_z  gaze_normal0_x  \\\n",
      "0       705.121116         20.000000  ...        -20.000000        0.011527   \n",
      "1       681.151453        -39.934928  ...        -20.075283       -0.136989   \n",
      "2      -298.890441         20.000000  ...        -20.000000        0.065145   \n",
      "3      -286.849309         20.000000  ...        -20.000000        0.067222   \n",
      "4      -288.102445         20.000000  ...        -20.000000        0.066338   \n",
      "\n",
      "   gaze_normal0_y  gaze_normal0_z  eye_center1_3d_x  eye_center1_3d_y  \\\n",
      "0        0.051002        0.998632               NaN               NaN   \n",
      "1        0.220475        0.965725        -39.934928         14.997919   \n",
      "2        0.027374        0.997500        -39.934928         14.997919   \n",
      "3        0.026625        0.997383        -39.934928         14.997919   \n",
      "4        0.023840        0.997512        -39.934928         14.997919   \n",
      "\n",
      "   eye_center1_3d_z  gaze_normal1_x  gaze_normal1_y  gaze_normal1_z  \n",
      "0               NaN             NaN             NaN             NaN  \n",
      "1        -20.075283       -0.145087        0.230744        0.962137  \n",
      "2        -20.075283       -0.145087        0.230744        0.962137  \n",
      "3        -20.075283       -0.152523        0.222659        0.962891  \n",
      "4        -20.075283       -0.152094        0.233980        0.960271  \n",
      "\n",
      "[5 rows x 21 columns]\n"
     ]
    }
   ],
   "source": [
    "def change_timestamp(timee):\n",
    "    ts = timee\n",
    "    datee = datetime.datetime.utcfromtimestamp(round(float(ts), 6))\n",
    "    time = datee.strftime('%Y-%m-%dT%H:%M:%S.%f0000')\n",
    "    return time\n",
    "\n",
    "file_read = pandas.read_csv('gaze.csv')\n",
    "file_read.insert(0, 'time_index', file_read['gaze_timestamp'].apply(change_timestamp))\n",
    "file_read['time_index'] = pandas.to_datetime(file_read['time_index'])\n",
    "file_read.set_index('time_index', inplace=True)\n",
    "file_read = file_read.resample('0.01S').first().reset_index(drop=True)\n",
    "print(file_read.head())"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "34b06f08d1ca6d2ae23a40d3b3aa5a1ecaa654c61e37ff02909888913051345e"
  },
  "kernelspec": {
   "display_name": "Python 3.10.2 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.2"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
